<!-- file groupdiv -->
<script> 
	
	$(function(){
	
		/***********************************************
		
			field.datepicker({ dateFormat : 'dd-mm-yy'});
			field.getCombobox({ task : "task"},"id","name");

		************************************************/
		
	
		/* REMOVE DUPLICATE ID */
		var ids = [];
		$('*').each(function() {
		    if (this.id && this.id !== '') {
		        if (ids[this.id]) {
		            $(this).remove();
		        } else {
		            ids[this.id] = this
		        }
		    }
		});
		
		$.fn.getCombobox = function(data, key, label){
			var field = $(this);
			$.ajax({
				url : "c_public_function",
				data : data,
				type : 'POST', dataType : 'json'
			}).done(function(response){
				for(i=0;i<response.length;i++){
					field.append("<option value="+response[i][key]+">"+response[i][label]+"</option>");
				}
					field.chosen();
			});
		}
		
		/* external function */
		
		$.fn.serializeObject = function()
		{
		   var o = {};
		   var a = this.serializeArray();
		   $.each(a, function() {
			   if (o[this.name]) {
				   if (!o[this.name].push) {
					   o[this.name] = [o[this.name]];
				   }
				   o[this.name].push(this.value || '');
			   } else {
				   o[this.name] = this.value || '';
			   }
		   });
		   return o;
		};
		
		/* declarisasi */
		
		task_groupdiv = $("#task_groupdiv");
		grid_groupdiv = $("#grid-groupdiv");
		pgrid_groupdiv = $("#pgrid-groupdiv");
		btnadd_groupdiv = $("#btn-add-groupdiv");
		btnedit_groupdiv = $("#btn-edit-groupdiv");
		btndelete_groupdiv = $("#btn-delete-groupdiv");
		btncancel_groupdiv =  $("#btn-cancel-groupdiv");
		btnsave_groupdiv = $("#btn-save-groupdiv");
		form_groupdiv = $("#form-groupdiv");
		
		field_groupdiv_id = $("#groupdiv_id");
		field_groupdiv_nama = $("#groupdiv_nama");
		field_divisi = $("#divisi");
		/* method started */
		
		$( "input[type=submit],input[type=button],input[type=reset], button" ).button();
			
		/* make component */
		
		grid_groupdiv.jqGrid({ 
			ajaxGridOptions : {type:"POST"},
			serializeGridData : function(postdata) {return postdata; },
			postData : {task : 'LIST'},
            url: 'c_groupdiv',
			datatype: "json", 
			colNames:[
				'Nama groupdivisi',
				'groupdiv_id'],
			colModel:[
				{ name: 'groupdiv_nama'  ,index: 'groupdiv_nama' , search : true},
				{ name: 'groupdiv_id'  ,index: 'groupdiv_id',key: true, hidden: true , search : false}
			],
			width: '700',
			height: 'auto',
			rowNum:10, 
			rowList:[10,20,30], 
			pager: '#pgrid-groupdiv', 
			sortname: 'groupdiv_id', 
			viewrecords: true, 
			sortorder: "desc",
			caption:"" 
		}); 
		grid_groupdiv.jqGrid('navGrid','#pgrid-groupdiv',{edit:false,add:false,del:false,search:false});
		grid_groupdiv.jqGrid('filterToolbar',{});
		
		form_groupdiv.dialog({
			autoOpen: false,
			height: "auto",
			width: "auto",
			modal: true
		});
		
		/* event */

		btnedit_groupdiv.click(function(){
			id = grid_groupdiv.jqGrid("getGridParam","selrow");
			if (id != null){
				task_groupdiv.val("UPDATE");
				dataRow = grid_groupdiv.getRowData(id);
				field_groupdiv_id.val(dataRow.groupdiv_id);
				field_groupdiv_nama.val(dataRow.groupdiv_nama);
				
                $.ajax({
					url	 : 'c_users',
					data : { task : 'listdiv' },
					type : 'POST',
					dataType : 'json'
				}).done(function(response){
					flow_id = 0;
					tr = "";
					field_divisi.find("tbody").html("");
					for(i=0;i<response.length;i++){
						// console.log(response[i]);
						var groupdiv_id = dataRow.groupdiv_id;
						var checkbox = "<input type='checkbox' name='divisi[]' value='"+response[i].flowdet_id+"' "; 
						console.log(groupdiv_id);
						if (response[i].s_users_id == groupdiv_id){
							checkbox = checkbox + " checked=true ";
						}else if (response[i].s_users_id != "" && response[i].s_users_id != null){
							checkbox = checkbox + " disabled=disabled ";
						}else{
						
						}
						
						checkbox = checkbox + " > ";
						
						if (flow_id != response[i].flow_id){ 
							tr = $("<tr></tr>");
							tr.append("<td>"+response[i].flow_name+"</td><td>"+
							checkbox+
							response[i].div_name+"</td>");
							field_divisi.find("tbody").append(tr);
						}else{
							tr.append("<td>"+
							checkbox+
							response[i].div_name+"</td>");
						}
						flow_id = response[i].flow_id;
					}
					
                    form_groupdiv.dialog("open");
				});
			}
		});
		
		btndelete_groupdiv.click(function(){
			id = grid_groupdiv.jqGrid("getGridParam","selrow");
			if (id != null){
				confr = confirm("Apa Anda yakin menghapus data ini?");
				if (confr){
					$.ajax({
						type: 'POST',
						url: 'c_groupdiv',
						data: {groupdiv_id: id, task : 'DELETE'},
						success: function(response){
							if(response==1){
								grid_groupdiv.trigger('reloadGrid');
								$("#grid-groupdiv").trigger('reloadGrid');
							}
						}
					});
				}
			}
		});
		
		btnadd_groupdiv.click(function(){
			btncancel_groupdiv.click();
			task_groupdiv.val("INSERT");
			form_groupdiv.dialog("open");
		});
		
		btncancel_groupdiv.click(function(){
			form_groupdiv.dialog("close");
		});
		
		btnsave_groupdiv.click(function(){
			data = form_groupdiv.serializeObject();
			$.ajax({
				url : "c_groupdiv",
				data : data,
				type : 'POST'
			}).done(function(response){
				grid_groupdiv.trigger('reloadGrid');
				btncancel_groupdiv.click();
			});
		});
		
	});
</script>

	<table id="grid-groupdiv" class="table table-striped table-bordered" ></table>
	<groupdiv id="pgrid-groupdiv"></groupdiv>
	
	<!-- button id="btn-add-groupdiv">Tambah</button -->
	<button id="btn-edit-groupdiv">Ubah</button>
	<button id="btn-delete-groupdiv">Hapus</button>
	
	<form id="form-groupdiv" title="groupdiv" disabled=true>
		<input type="hidden" value="INSERT" name="task" id="task_groupdiv">
	   <table>
			<tr style="display: none">
				<td align="right">groupdiv_id:</td>
				<td align="left"><input id="groupdiv_id" name="groupdiv_id" /></td>
			</tr>
			
			<tr > <td align="right">Nama groupdivisi:</td> <td align="left"><input id="groupdiv_nama" name="groupdiv_nama" /></td> </tr>
			<tr valign=top id="tr_divisi" > <td align="right">Divisi:</td> <td align="left"><table id="divisi" ><thead>
			<th>Dokumen Nama</th>
			<th>Divisi</th>
			</thead>
			<tbody>
			</tbody>
			</table></td> </tr>
			<tr>
				<td align="right"></td>
				<td style="padding-top: 10px;" align="right">
					<input style="margin-right: 5px;" type="button" id="btn-save-groupdiv" value="Simpan"  />
					<input id="btn-cancel-groupdiv" type="reset" value="Batal" /></td>
			</tr>
		</table>
	</form>